package pe.tnova.adra.kernel.log;

import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import pe.tnova.adra.util.exception.KernelException;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.FileAppender;
//http://fahdshariff.blogspot.com/2010/10/logback-change-root-logger-level.html
//http://stackoverflow.com/questions/5448673/slf4j-logback-how-to-configure-loggers-in-runtime
//http://www.programcreek.com/java-api-examples/index.php?api=ch.qos.logback.classic.LoggerContext
//http://logback.qos.ch/manual/introduction.html


//http://stackoverflow.com/questions/16910955/programmatically-configure-logback-appender
@Component("LogEstructura")
@SuppressWarnings({ "rawtypes", "deprecation" })
public class LogEstructuraIMpl implements LogEstructura {

	private static final org.slf4j.Logger log = LoggerFactory.getLogger(LogEstructuraIMpl.class);

	public void actulizarLog(String[] selectLogs) throws KernelException {
		// DOMConfigurator.configure("log4j.xml");
		LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
		List<Logger> l = lc.getLoggerList();

		for (Logger rootLogger : l) {
			if (rootLogger.getLevel() == null) {
				break;
			}
			System.out.println(rootLogger);
			LoggerContext loggerContext = rootLogger.getLoggerContext();
			loggerContext.reset();
			loggerContext.resetTurboFilterList();
			PatternLayoutEncoder encoder = new PatternLayoutEncoder();
			encoder.setContext(loggerContext);
			// encoder.setPattern("%-5level [%thread]: %message%n");
			encoder.setPattern("%d{ABSOLUTE} %5p %40.40c:%4L - %m%n");
			encoder.start();
			ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>();
			appender.setContext(loggerContext);
			appender.setEncoder(encoder);
			appender.start();
			rootLogger.addAppender(appender);
			rootLogger.setLevel(Level.INFO);// cambio de nivel
			System.out.println("interno");
			System.out.println(rootLogger.isInfoEnabled());
			System.out.println(rootLogger.isDebugEnabled());
			System.out.println(rootLogger.isErrorEnabled());
			System.out.println(rootLogger.isTraceEnabled());
			System.out.println(rootLogger.isWarnEnabled());
			rootLogger.debug("Message 1");
			rootLogger.info("Message info");
			rootLogger.warn("Message 2");
			rootLogger.error("error");
			rootLogger.trace("msg trace");
			System.out.println("externo");

			FileAppender<ILoggingEvent> appenderFile = new FileAppender<ILoggingEvent>();
			appenderFile.setContext(loggerContext);
			appenderFile.setEncoder(encoder);
			appenderFile.start();
//			appenderFile.setFile(file);
			
			rootLogger.addAppender(appenderFile);
		}

	}

	public void ejecutarTesinServerLog() throws KernelException {
		System.out.println("ejecutarTesinServerLog");
		log.info("info msg");
		log.debug("debug msg");
		log.warn("warn msg");
		log.error("error msg");
		log.trace("msg trace");
	}

}
